

#import "UserTool.h"

@implementation UserTool

+ (void)login:(NSString *)acount psw:(NSString*)psw complete:(nullable void(^) (NSDictionary * dict))block{
    [self find:acount complete:^(User * _Nonnull user) {
        if (user == nil) {
            if (block) { return block(@{@"code":@"201",@"msg":@"该账号不存在"});}
        }else{
            if ([user.password isEqualToString:psw] == NO) {
                if (block) {return block(@{@"code":@"201",@"msg":@"密码不对"});}
            }else{
                if (block){ return block(@{@"code":@"200",@"data":user});}
            }
        }
    }];
}

+ (void)register:(NSString*)account psw:(NSString*)psw complete:(nullable void(^) (NSDictionary *dict))block{
    [self register:@"未设置昵称" account:account psw:psw complete:block];
}

+ (void)register:(NSString*)username account:(NSString*)account psw:(NSString*)psw complete:(nullable void(^) (NSDictionary *dict))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"account"),bg_sqlValue(account)];
    NSInteger cout = [User bg_count:nil where:where];
    if (cout > 0) {
        if (block) { return block(@{@"code":@"201",@"msg":@"该账号已存在"});}
    }
    
    NSInteger count = [User bg_count:nil where:nil];
    
    User *u = [User new];
    u.password = psw;
    u.account = account;
    u.ID = [NSString stringWithFormat:@"%d",(int)cout + 1];
    u.headImg = UIImagePNGRepresentation([UIImage imageNamed:@"默认头像"]);
    u.username = username;
    u.createTime = [NSDate currentTimeStamp:@"yyyy/MM/dd HH:mm:ss"];
    u.token = [NSString stringWithFormat:@"%@%@",account,psw].md5;
    u.gender = @"男";
    u.age = @"100";
    [u bg_saveOrUpdateAsync:^(BOOL isSuccess) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (isSuccess){
                if (block) { return block(@{@"code":@"200",@"data":@{@"account":account,@"token":u.token}});}
            }else{
                if (block) { return block(@{@"code":@"201",@"msg":@"注册失败"});}
            }
        });
    }];
}
+ (void)update:(User *)user complete:(nullable void(^) (NSDictionary *dict))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"account"),bg_sqlValue(user.account)];
    NSInteger count = [User bg_count:nil where:where];
    if (count <= 0) {
        if (block) { return block(@{@"code":@"201",@"msg":@"该账号不存在"});}
    }
    [user bg_saveOrUpdateAsync:^(BOOL isSuccess) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (isSuccess){
                if (block) { return block(@{@"code":@"200",@"msg":@"保存成功"});}
            }else{
                if (block) { return block(@{@"code":@"201",@"msg":@"保存失败"});}
            }
        });
    }];
}
+ (void)delete:(User *)user complete:(nullable void(^) (NSDictionary *dict))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"account"),bg_sqlValue(user.account)];
    NSInteger count = [User bg_count:nil where:where];
    if (count <= 0) {
        if (block) { return block(@{@"code":@"201",@"msg":@"该账号不存在"});}
    }
    [User bg_deleteAsync:nil where:where complete:^(BOOL isSuccess) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (isSuccess){
                if (block) { return block(@{@"code":@"200",@"msg":@"注销成功"});}
            }else{
                if (block) { return block(@{@"code":@"201",@"msg":@"注销失败"});}
            }
        });
    }];
}
+ (void)find:(NSString*)account complete:(nullable void(^) (User *user))block{
    NSString* where = [NSString stringWithFormat:@"where %@=%@",bg_sqlKey(@"account"),bg_sqlValue(account)];
    NSInteger cout = [User bg_count:nil where:where];
    if (cout <= 0) {
        if (block) { return block(nil);}
    }
    __block User *u;
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        [User bg_findAsync:nil where:where complete:^(NSArray * _Nullable array) {
            dispatch_async(dispatch_get_main_queue(), ^{
                u = array[0];
                dispatch_group_leave(group);
            });
        }];
    });
    
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        if (block) {
            block(u);
        }
    });
}
@end
